package com.alibaba.algo.controller.user;

import com.alibaba.algo.annotation.OperateLogAnnotation;
import com.alibaba.algo.apiimpl.user.SysRoleServiceImpl;
import com.alibaba.algo.cooperation.common.BaseResult;
import com.alibaba.algo.cooperation.common.SysConstant;
import com.alibaba.algo.cooperation.usercenter.GlobalResultStatus;
import com.alibaba.algo.cooperation.usercomponent.vo.req.user.*;
import com.alibaba.algo.cooperation.usercomponent.vo.res.user.SelectRoleDetailByIdResponseVO;
import com.alibaba.algo.enums.OperateTypeEnum;
import com.alibaba.algo.enums.ResourceTypeEnum;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 类SysRoleController.java的实现描述：角色 Controller
 *
 * @author nieganggang 2021-03-12 16:08:17
 */
@RestController
@RequestMapping(value = "/usercenter-admin-app/admin/sysRole")
@Api(value = "角色管理", tags = "角色管理")
@Slf4j
public class SysRoleController {

    @Autowired
    private SysRoleServiceImpl sysRoleService;

    /**
     * 新增组织
     *
     * @return
     */
    @PostMapping("/insert")
    @ApiOperation(value = "公共角色管理新增角色")
    @OperateLogAnnotation(resouceType = ResourceTypeEnum.ROLE, operateType = OperateTypeEnum.INSERT)
    public BaseResult insert(@RequestBody CreateRoleRequestVO vo) {
        if (StringUtils.isEmpty(vo.getRoleCode())) {
            return BaseResult.buildError(GlobalResultStatus.PARAM_ROLE_CODE_ERROR.getCode(), GlobalResultStatus.PARAM_ROLE_CODE_ERROR.getMsg());
        }
        if (StringUtils.isEmpty(vo.getRoleName())) {
            return BaseResult.buildError(GlobalResultStatus.PARAM_ROLE_NAME_ERROR.getCode(), GlobalResultStatus.PARAM_ROLE_NAME_ERROR.getMsg());
        }
        if (StringUtils.isEmpty(vo.getAppCode())) {
            return BaseResult.buildError(GlobalResultStatus.PARAM_APP_CODE_ERROR.getCode(), GlobalResultStatus.PARAM_APP_CODE_ERROR.getMsg());
        }
        return sysRoleService.insert(vo);
    }

    @PostMapping("/insertOrgRole")
    @ApiOperation(value = "组织角色管理新增角色")
    @OperateLogAnnotation(resouceType = ResourceTypeEnum.ROLE, operateType = OperateTypeEnum.INSERT)
    public BaseResult insertOrgRole(@RequestBody CreateRoleRequestVO vo) {
        if (StringUtils.isEmpty(vo.getRoleCode())) {
            return BaseResult.buildError(GlobalResultStatus.PARAM_ROLE_CODE_ERROR.getCode(), GlobalResultStatus.PARAM_ROLE_CODE_ERROR.getMsg());
        }
        if (StringUtils.isEmpty(vo.getRoleName())) {
            return BaseResult.buildError(GlobalResultStatus.PARAM_ROLE_NAME_ERROR.getCode(), GlobalResultStatus.PARAM_ROLE_NAME_ERROR.getMsg());
        }
        if (StringUtils.isEmpty(vo.getAppCode())) {
            return BaseResult.buildError(GlobalResultStatus.PARAM_APP_CODE_ERROR.getCode(), GlobalResultStatus.PARAM_APP_CODE_ERROR.getMsg());
        }
        return sysRoleService.insertOrgRole(vo);
    }

    @PostMapping("/delete")
    @ApiOperation(value = "角色管理删除角色")
    @OperateLogAnnotation(resouceType = ResourceTypeEnum.ROLE, operateType = OperateTypeEnum.DELETE)
    public BaseResult delete(@RequestBody List<Long> vo) {
        if (CollectionUtils.isEmpty(vo)) {
            return BaseResult.buildError(GlobalResultStatus.PARAM_ROLE_ID_ERROR.getCode(), GlobalResultStatus.PARAM_ROLE_ID_ERROR.getMsg());
        }
        return sysRoleService.delete(vo);
    }


    @PostMapping("/update")
    @ApiOperation(value = "公共角色管理更新角色")
    @OperateLogAnnotation(resouceType = ResourceTypeEnum.ROLE, operateType = OperateTypeEnum.UPDATE)
    public BaseResult update(@RequestBody UpdateRoleRequestVO vo) {
        if (StringUtils.isEmpty(vo.getId())) {
            return BaseResult.buildError(GlobalResultStatus.PARAM_ROLE_ID_ERROR.getCode(), GlobalResultStatus.PARAM_ROLE_ID_ERROR.getMsg());
        }
        if (StringUtils.isEmpty(vo.getRoleCode())) {
            return BaseResult.buildError(GlobalResultStatus.PARAM_ROLE_CODE_ERROR.getCode(), GlobalResultStatus.PARAM_ROLE_CODE_ERROR.getMsg());
        }
        if (StringUtils.isEmpty(vo.getRoleName())) {
            return BaseResult.buildError(GlobalResultStatus.PARAM_ROLE_NAME_ERROR.getCode(), GlobalResultStatus.PARAM_ROLE_NAME_ERROR.getMsg());
        }
        if (StringUtils.isEmpty(vo.getAppCode())) {
            return BaseResult.buildError(GlobalResultStatus.PARAM_APP_CODE_ERROR.getCode(), GlobalResultStatus.PARAM_APP_CODE_ERROR.getMsg());
        }
        return sysRoleService.update(vo);
    }

    @PostMapping("/updateRoleBasicInfo")
    @ApiOperation(value = "公共角色管理更新角色基本信息")
    @OperateLogAnnotation(resouceType = ResourceTypeEnum.ROLE, operateType = OperateTypeEnum.UPDATE)
    public BaseResult updateRoleBasicInfo(@RequestBody UpdateRoleRequestVO vo) {
        if (StringUtils.isEmpty(vo.getId())) {
            return BaseResult.buildError(GlobalResultStatus.PARAM_ROLE_ID_ERROR.getCode(), GlobalResultStatus.PARAM_ROLE_ID_ERROR.getMsg());
        }
        if (StringUtils.isEmpty(vo.getRoleCode())) {
            return BaseResult.buildError(GlobalResultStatus.PARAM_ROLE_CODE_ERROR.getCode(), GlobalResultStatus.PARAM_ROLE_CODE_ERROR.getMsg());
        }
        if (StringUtils.isEmpty(vo.getRoleName())) {
            return BaseResult.buildError(GlobalResultStatus.PARAM_ROLE_NAME_ERROR.getCode(), GlobalResultStatus.PARAM_ROLE_NAME_ERROR.getMsg());
        }
        if (StringUtils.isEmpty(vo.getAppCode())) {
            return BaseResult.buildError(GlobalResultStatus.PARAM_APP_CODE_ERROR.getCode(), GlobalResultStatus.PARAM_APP_CODE_ERROR.getMsg());
        }
        return sysRoleService.updateRoleBasicInfo(vo);
    }

    @PostMapping("/updateRoleUser")
    @ApiOperation(value = "公共角色管理更新角色")
    @OperateLogAnnotation(resouceType = ResourceTypeEnum.ROLE, operateType = OperateTypeEnum.UPDATE)
    BaseResult updateRoleUser(@RequestBody UpdateRoleRequestVO vo,@RequestParam(name="flag",defaultValue = "0")int flag){
        if (StringUtils.isEmpty(vo.getId())) {
            return BaseResult.buildError(GlobalResultStatus.PARAM_ROLE_ID_ERROR.getCode(), GlobalResultStatus.PARAM_ROLE_ID_ERROR.getMsg());
        }

        if(vo.getUserIdList()==null){
            return BaseResult.buildError(GlobalResultStatus.PARAM_USER_ID_ERROR.getCode(), GlobalResultStatus.PARAM_USER_ID_ERROR.getMsg());
        }
        return sysRoleService.updateRoleUser(vo,flag);
    }

    @PostMapping("/updateOrgRole")
    @ApiOperation(value = "组织角色管理更新角色")
    @OperateLogAnnotation(resouceType = ResourceTypeEnum.ROLE, operateType = OperateTypeEnum.UPDATE)
    public BaseResult updateOrgRole(@RequestBody UpdateRoleRequestVO vo) {
        if (StringUtils.isEmpty(vo.getId())) {
            return BaseResult.buildError(GlobalResultStatus.PARAM_ROLE_ID_ERROR.getCode(), GlobalResultStatus.PARAM_ROLE_ID_ERROR.getMsg());
        }
        if (StringUtils.isEmpty(vo.getRoleCode())) {
            return BaseResult.buildError(GlobalResultStatus.PARAM_ROLE_CODE_ERROR.getCode(), GlobalResultStatus.PARAM_ROLE_CODE_ERROR.getMsg());
        }
        if (StringUtils.isEmpty(vo.getRoleName())) {
            return BaseResult.buildError(GlobalResultStatus.PARAM_ROLE_NAME_ERROR.getCode(), GlobalResultStatus.PARAM_ROLE_NAME_ERROR.getMsg());
        }
        if (StringUtils.isEmpty(vo.getAppCode())) {
            return BaseResult.buildError(GlobalResultStatus.PARAM_APP_CODE_ERROR.getCode(), GlobalResultStatus.PARAM_APP_CODE_ERROR.getMsg());
        }
        return sysRoleService.updateOrgRole(vo);
    }

    @PostMapping("/detail")
    @ApiOperation(value = "公共角色管理查看角色详情")
    public BaseResult detail(@RequestBody SelectRoleDetailByIdRequestVO vo) {
        if (ObjectUtils.isEmpty(vo.getRoleId())) {
            return BaseResult.buildError(GlobalResultStatus.PARAM_ROLE_ID_ERROR.getCode(), GlobalResultStatus.PARAM_ROLE_ID_ERROR.getMsg());
        }
        SelectRoleDetailByIdResponseVO result = sysRoleService.selectDetailById(vo);
        return BaseResult.buildSuccess(result);
    }

    @PostMapping("/detailOrgRole")
    @ApiOperation(value = "组织角色管理查看角色详情")
    public BaseResult detailOrgRole(@RequestBody SelectRoleDetailByIdRequestVO vo) {
        if (ObjectUtils.isEmpty(vo.getRoleId())) {
            return BaseResult.buildError(GlobalResultStatus.PARAM_ROLE_ID_ERROR.getCode(), GlobalResultStatus.PARAM_ROLE_ID_ERROR.getMsg());
        }
        SelectRoleDetailByIdResponseVO result = sysRoleService.selectOrgRoleDetailById(vo);
        return BaseResult.buildSuccess(result);
    }


    @PostMapping("/selectPage")
    public BaseResult selectPage(@RequestBody SelectRoleListRequestVO vo) {
        BaseResult check = check(vo);
        if (!SysConstant.OK.equals(check.getCode())) {
            return check;
        }
        return sysRoleService.selectPage(vo);
    }


    @PostMapping("/permissionSelect")
    public BaseResult permissionSelect(@RequestBody SelectRoleListRequestVO vo) {
        BaseResult check = check(vo);
        if (!SysConstant.OK.equals(check.getCode())) {
            return check;
        }
        return sysRoleService.permissionSelect(vo);
    }

    @PostMapping("/selectOrgRolePage")
    public BaseResult selectOrgRolePage(@RequestBody SelectRoleListRequestVO vo) {
        BaseResult check = check(vo);
        if (!SysConstant.OK.equals(check.getCode())) {
            return check;
        }
        return sysRoleService.selectOrgRolePage(vo);
    }

    private BaseResult check(SelectRoleListRequestVO vo) {
        if (ObjectUtils.isEmpty(vo.getPageIndex())) {
            return BaseResult.buildError(GlobalResultStatus.PARAM_PAGE_INDEX_ERROR.getCode(), GlobalResultStatus.PARAM_PAGE_INDEX_ERROR.getMsg());
        }
        if (ObjectUtils.isEmpty(vo.getPageSize())) {
            return BaseResult.buildError(GlobalResultStatus.PARAM_PAGE_SIZE_ERROR.getCode(), GlobalResultStatus.PARAM_PAGE_SIZE_ERROR.getMsg());
        }
        if (StringUtils.isEmpty(vo.getAppCode())) {
            return BaseResult.buildError(GlobalResultStatus.PARAM_APP_CODE_ERROR.getCode(), GlobalResultStatus.PARAM_APP_CODE_ERROR.getMsg());
        }
        return BaseResult.buildSuccess();
    }

    @PostMapping("/selectList")
    public BaseResult selectList(@RequestBody SelectRoleListByAppRequestVO vo) {
        if (StringUtils.isEmpty(vo.getAppCode())) {
            return BaseResult.buildError(GlobalResultStatus.PARAM_APP_CODE_ERROR.getCode(), GlobalResultStatus.PARAM_APP_CODE_ERROR.getMsg());
        }
        return sysRoleService.selectList(vo);
    }

    @PostMapping("/selectRoleListByKey")
    public BaseResult selectRoleListByKey(@RequestBody SelectRoleDetailByCodeRequestVO vo) {
        if (StringUtils.isEmpty(vo.getAppCode())) {
            return BaseResult.buildError(GlobalResultStatus.PARAM_APP_CODE_ERROR.getCode(), GlobalResultStatus.PARAM_APP_CODE_ERROR.getMsg());
        }
        return sysRoleService.selectRoleListByKey(vo);
    }

    @PostMapping("/updateUserRoleRelatedByOrgRole")
    public BaseResult updateUserRoleRelatedByOrgRole() {
        log.info("updateUserRoleRelatedByOrgRole begin");
        BaseResult result = sysRoleService.updateUserRoleRelatedByOrgRole();
        log.info("updateUserRoleRelatedByOrgRole end, result:{}", JSON.toJSONString(result));
        return result;
    }
}
